#!/usr/bin/php
<?php

/**
 * Test script for FineDB.
 *
 * @author	Amaury Bouchard <amaury.bouchard@finemedia.fr>
 * @copyright	Copyright (c) 2010, FineMedia
 * @package	FineDB
 * @subpackage	test
 */

require_once("../lib/php/class.FineDB.php");
require_once("../lib/php/class.FineDBDocument.php");

/** Class used to map the users collection. */
class MyUsers extends FineDBCollection {
	public function __construct($base, $collection, $indexes, $mandatory, $documentObjectName=null) {
		parent::__construct($base, $collection, $indexes, $mandatory, "MyUser");
	}
	/**
	 * Returns a user from its unique identifier.
	 * @param	string	$id	The user's identifier.
	 * @return	MyUser	The user.
	 */
	public function get($id) {
		return ($this->_get($id));
	}
	/**
	 * Get a user from its email address.
	 * @param	string	$email	The user's email address.
	 * @return	MyUser	The user.
	 */
	public function getUserFromEmail($email) {
		return ($this->_getFromIndex("email", $email));
	}
	/**
	 * Search a list of documents from some criteria.
	 * @param	array		$criteria	Hash of search criteria.
	 * @param	string|array	$sort		(optional) Hash of sort criteria.
	 * @param	int		$limitOffset	(optional) Offset of the first returned document. Can be negative to
	 *						specify an offset from the end of the list. 0 by default.
	 * @param	int		$nbrDocuments	(optional) Number of documents to return. By default, returns all documents after the offset.
	 * @return	array		List of document identifiers.
	 * @throws	ApplicationException	Parameter error.
	 * @throws	IOException		Read error.
	 */
	public function search($criteria, $sort=null, $limitOffset=null, $nbrDocuments=null) {
		return ($this->_search($criteria, $sort, $limitOffset, $nbrDocuments));
	}
}

/** Class used to map the users documents to the object model. */
class MyUser extends FineDBDocument {
	/**
	 * Set the email address of this user.
	 * @param	string	$email	The new email address.
	 */
	public function setEmail($email) {
		$this->setMeta("email", $email);
		$this->save();
	}
	/**
	 * Get the email address of this user.
	 * @return	string	The user's email address.
	 */
	public function getEmail() {
		return ($this->_getMeta("email"));
	}
	/**
	 * Get the name of this user.
	 * @return	string	The user's name.
	 */
	public function getName() {
		return ($this->_getData("name"));
	}
	/**
	 * Set the name of this user.
	 * @param	string	$name	User's name.
	 */
	public function setName($name) {
		$this->_setData("name", $name);
		$this->_save();
	}
}

// DB connection
$base = new FineDb("test");
// collection connection
$users = $base->getCollection("users", "MyUsers");
// object mapping
//$users->setDocumentObject("MyUser");

// get one user
print("--------------------\nGET\n");
$user = $users->getUserFromEmail("john@john.com");
print("username : '" . $user->getName() . "'\n");

// search
print("--------------------\nSEARCH\n");
$criteria = array(
	/*
	array(
		'index'  => "email",
		'search' => array(
			"lyn@da.com",
			"john@john.com"
		)
	),
	*/
	array(
		'index'  => "nickname",
		'search' => array(
			"James",
			"Johnny"
		)
	),
	'gender' => "male"
);
$result = $users->search($criteria);
print_r($result);
foreach ($result as $documentId) {
	$user = $users->get($documentId);
	$email = $user->getEmail();
	print("email : '$email'\n");
}

?>
